package
{
	import com.greensock.TweenLite;
	import com.sociodox.theminer.TheMiner;
	import org.despair2D.control.TouchManager;
	import org.despair2D.ui.events.PanelEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.Capabilities;
	import flash.utils.getQualifiedClassName;
	
	import assets.AssetsCore;
	import assets.AssetsUI;
	
	import org.despair2D.Despair;
	import org.despair2D.StatsKai;
	import org.despair2D.control.KeyboardManager;
	import org.despair2D.resource.EmbededUtil;
	import org.despair2D.resource.LoaderManager;
	import org.despair2D.ui.DespairUI;
	import org.despair2D.ui.core.IPanel;
	import org.despair2D.utils.getClassName;
	import org.despair2D.ui.ButtonType;
	
	import states.*;

	[SWF(width = '960' ,height = '600', frameRate = '60')]
public class DespairUITest extends Sprite 
{
	
	public function DespairUITest() 
	{
		this.addEventListener(Event.ADDED_TO_STAGE, init)
	}
	
	public var mStateList:Array =
	[
		ImageState,
		StartingState,
		AnimeState,
		NineScaleState,
		TextUIState,
		RangeUIState,
		MultiUIState
	]
	
	
	private function init( e:Event ) : void
	{
		this.removeEventListener(Event.ADDED_TO_STAGE, init)
		
		// Engine !!
		Despair.startup(this.stage)
		
		// UI...
		DespairUI.startup(false, 960, 600)
		
		// keyboard...
		KeyboardManager.getInstance().initialize()
		
		// Stats
		this.addChild(new StatsKai)
		//var tm:TheMiner = new TheMiner()
		//this.addChild(tm)
		
		// Assets...
		var assetsList:Array
		var i:int, l:int
		
		
		
		assetsList = AssetsCore.getAssetList().concat(AssetsUI.getAssetList())
		l = assetsList.length
		while (--l > -1)
		{
			LoaderManager.getInstance().getBytesLoader(assetsList[l])
		}
		
		LoaderManager.getInstance().addCompleteListener(onAssetsComplete)
	}
	
	private function onAssetsComplete() : void
	{
		//EmbededUtil.dispose()
		LoaderManager.getInstance().reomveCompleteListener(onAssetsComplete)
		
		DespairUI.addImageButtonData(AssetsUI.AT_btn_yellow, 'AT_btn_yellow_1', ButtonType.BUTTON_HAS_INVALID | ButtonType.LEAVE_LEAVE)
		DespairUI.addImageButtonData(AssetsUI.AT_btn_yellow, 'AT_btn_yellow_2', ButtonType.BUTTON_HAS_INVALID | ButtonType.LEAVE_PRESS)
		DespairUI.addImageButtonData(AssetsUI.AT_btn_yellow, 'AT_btn_yellow_3', ButtonType.BUTTON_HAS_INVALID | ButtonType.PRESS_PRESS)
		DespairUI.addImageButtonData(AssetsUI.AT_checkbox, 'AT_checkbox_1', ButtonType.CHECKBOX_HAS_INVALID | ButtonType.LEAVE_LEAVE)
		DespairUI.addImageButtonData(AssetsUI.AT_checkbox, 'AT_checkbox_2', ButtonType.CHECKBOX_HAS_INVALID | ButtonType.LEAVE_PRESS)
		DespairUI.addImageButtonData(AssetsUI.AT_checkbox, 'AT_checkbox_3', ButtonType.CHECKBOX_HAS_INVALID | ButtonType.PRESS_PRESS)
		
		DespairUI.addMovieClipButtonData('Btn_A', 'Btn_A', ButtonType.BUTTON_HAS_INVALID | ButtonType.LEAVE_LEAVE)
		DespairUI.addMovieClipButtonData('Select_A', 'Select_A', ButtonType.CHECKBOX_HAS_INVALID | ButtonType.LEAVE_LEAVE)
		DespairUI.addNineScaleData((new AssetsUI.scroll).bitmapData, 'scroll')
		
		// Section...
		var sections:Array = AssetsCore.getSectionXML()
		var i:int, l:int = sections.length
		var stateType:Class
		var name:String
		
		while (i < l)
		{
			Despair.registerSections(XML(sections[i++]))
		}
		
		mLength = mStateList.length
		l = mStateList.length
		
		while(--l>-1)
		{
			stateType = mStateList[l];
			name = getClassName(stateType)
			DespairUI.registerPanel(name, stateType)
			DespairUI.getPanel(name).addEventListener(PanelEvent.POPUP, function(e:PanelEvent):void
			{
				trace(DespairUI.numComp)
			})
		}
		
		// Keyboard !!
		KeyboardManager.getInstance().getState().addPressListener('K', function():void
		{
			DespairUI.killAllPanels()
		})
		KeyboardManager.getInstance().getState().addPressListener('T', function():void
		{
			TweenLite.paused = !TweenLite.paused
		})
		KeyboardManager.getInstance().getState().addPressListener('UP', function():void
		{
			Despair.timeFactor -= 0.1
			trace(Despair.timeFactor)
		})
		KeyboardManager.getInstance().getState().addPressListener('DOWN', function():void
		{
			Despair.timeFactor += 0.1
			trace(Despair.timeFactor)
		})
		KeyboardManager.getInstance().getState().addPressListener('LEFT', function():void
		{
			if (--mIndex < 0)
			{
				mIndex=mLength - 1
			}
			changeState()
		})
		KeyboardManager.getInstance().getState().addPressListener('RIGHT', function():void
		{
			if (++mIndex >= mLength)
			{
				mIndex=0
			}
			changeState()
		})
		changeState()
		
		
		//Despair.addUpdateListener(function():void
		//{
			//trace(TouchManager.getInstance().isTouched)
			//trace(TouchManager.getInstance().isJustTouched)
		//})
	}
	
	private function changeState():void
	{
		var panel:IPanel
		var type:Class
		
		panel = DespairUI.getPanel(mPanelName)
		if (panel)
		{
			panel.close()
		}
		type = mStateList[mIndex] as Class
		
		mPanelName = getClassName(type)
		DespairUI.getPanel(mPanelName).popup(-1,true,[44])
		trace('[Panel] - ' + mPanelName)
	}



	private var mIndex:int

	private var mLength:int

	private var mPanelName:String
}

}